﻿using BookManage.BLL;
using BookManage.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BookManage
{
    internal enum opStatus
    {
        inSelect=0,  //查询操作状态
        inNew=1, //办理新借书证状态
        inChange= 2, //变更借书证状态
        inBorrow=3,
        inOther=4
    };
    public partial class frmReader : Form
    {
        private DataTable dt = null;//存放查询结果，并给DataGridView dgvReader提供数据
        private Reader reader = new Reader();//存放读者信息，与读者信息组控件内的各控件进行数据交换，并与BLL、Model层进行数据传递。
        private ReaderType readertype = new ReaderType();
        private ReaderAdmin readerBLL = new ReaderAdmin();
        private opStatus ops;//枚举类型，表示3种窗体的状态—查询、办理、变更
        private void SetStatus(opStatus opst)
        {
            ops = opst;
            switch (ops)
            {
                case opStatus.inSelect:
                    toolStrip1.Enabled = true;  //查询工具栏
                    groupBox1.Enabled = true;  //查询结果
                    groupBox2.Enabled = false;  //读者信息
                    break;
                case opStatus.inNew:
                    toolStrip1.Enabled = false;  //查询工具栏
                    groupBox1.Enabled = false;  //查询结果
                    groupBox2.Enabled = true;  //读者信息
                    btnAddReader.Enabled = true;  //确认办证
                    btnUpdateReader.Enabled = false;  //确认变更
                    break;
                case opStatus.inChange:
                    toolStrip1.Enabled = false; //查询工具栏
                    groupBox1.Enabled = false; //查询结果
                    groupBox2.Enabled = true; //读者信息
                    btnAddReader.Enabled = false;  //确认办证
                    btnUpdateReader.Enabled = true;  //确认变更
                    break;
            }
        }
        public frmReader()
        {
            InitializeComponent();
            dt = readerBLL.GetAllReaderType();
            foreach(DataRow dr in dt.Rows)
            {
                cmbTypeForQry.Items.Add(dr["rdType"].ToString() + "--" + dr["rdTypeName"].ToString());
                cmbType.Items.Add(dr["rdType"].ToString() + "--" + dr["rdTypeName"].ToString());
            }
            SetStatus(opStatus.inSelect);
            dt = readerBLL.GetReader(0, "", "");  //查询空记录集，仅得到读者表结构
            //dt = readerBLL.GetReader(rdType, rdDept, rdName); 
            //ShowData();
        }

        private void ShowData()
        {
            dgvReader.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
                dgvReader.Columns[dc.ColumnName].HeaderText = Reader.ColumnTitle(dc.ColumnName);
            }
        }
        
        private void frmReader_Load(object sender, EventArgs e)
        {

        }

        private void btnNewDoc_Click(object sender, EventArgs e)
        {
            SetStatus(opStatus.inNew);//办理借书证，跳到读者信息窗口，查询窗口不可用
        }

        private void btnChangeDoc_Click(object sender, EventArgs e)
        {
            SetStatus(opStatus.inChange);//变更借书证，跳到读者信息窗口，查询窗口不可用
        }

        private void btnCancelChange_Click(object sender, EventArgs e)
        {
           SetStatus(opStatus.inSelect);//撤销，跳到查询窗口，读者信息窗口不可用
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            int rdType;
            string rdDept, rdName;
            
            if (cmbTypeForQry.Text.Trim() == "") //读者类别没写
            {
                rdType = 0;
            }
            else
            {
                int i = cmbTypeForQry.Text.IndexOf("--");
                if (i > 0)
                    rdType = Convert.ToInt32(cmbTypeForQry.Text.Substring(0, i));
                else
                    rdType = Convert.ToInt32(cmbTypeForQry.Text);
            }
            rdDept = cmbDeptForQry.Text;
            rdName = txtNameForQry.Text;
            /*
                先调用readerBLL类中的GetReader(),GetReader()中又调用了ReaderDAL.GetReader()
            */
            dt = readerBLL.GetReader(rdType, rdDept, rdName);//ReaderDAL.GetReader(rdType, rdDept, rdName)的判断语句,无法判断单位和姓名
            ShowData();
        }

        private void btnAllQry_Click(object sender, EventArgs e)
        {
            dt=readerBLL.GetAllReader();
            ShowData();
        }
        private void btnToExcel_Click(object sender, EventArgs e)
        {
            ExcelHelper.ExportToExcel(dt);
        }
        private void dgvReader_SelectionChange(object sender,EventArgs e)
        {
            if (dgvReader.CurrentCell == null)
                return;
            reader = ReaderAdmin.GetReader(Convert.ToInt32(dgvReader["rdID", dgvReader.CurrentCell.RowIndex].Value.ToString()));
            SetReaderToText();
        }
        private void dgvReader_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void SetReaderToText()
        {
            txtID.Text = Convert.ToString(reader.rdID);
            txtName.Text = reader.rdName;
            txtPwd.Text = reader.rdPwd;
            cmbSex.Text = reader.rdSex;
            cmbType.Text = Convert.ToString(reader.rdType);
            cmbDept.Text = reader.rdDept;
            txtPhone.Text = reader.rdPhone;
            txtEmail.Text = reader.rdEmail;
            dtpDateReg.Value = reader.rdDateReg;
            if (reader.rdPhoto == null)
                picboxPhoto.Image = null;
            else
            {
                MemoryStream ms = new MemoryStream(reader.rdPhoto);
                Image imgPhoto = Bitmap.FromStream(ms, true);
                picboxPhoto.Image = imgPhoto;
            }
            txtStatus.Text = reader.rdStatus;
            txtBorrowQty.Text = Convert.ToString(reader.rdBorrowQty);
            txtAdminPoles.Text = Convert.ToString(reader.rdAdminRoles);
        }
        private void SetTextToReader()
        {
            reader.rdID = Convert.ToInt32(txtID.Text);
            reader.rdName = txtName.Text;
            reader.rdPwd = txtPwd.Text;
            reader.rdSex = cmbSex.Text;
            int i = cmbType.Text.IndexOf("--");
            if (i > 0)
                reader.rdType = Convert.ToInt32(cmbType.Text.Substring(0, i));
            else
                reader.rdType = Convert.ToInt32(cmbType.Text);
            reader.rdDept = cmbDept.Text;
            reader.rdPhone = txtPhone.Text;
            reader.rdEmail = txtEmail.Text;
            reader.rdDateReg = dtpDateReg.Value;
            if (picboxPhoto.Image != null)
            {
                MemoryStream ms = new MemoryStream();
                picboxPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                reader.rdPhoto = ms.GetBuffer();
            }
            reader.rdStatus = txtStatus.Text;
            reader.rdBorrowQty = Convert.ToInt32(txtBorrowQty.Text);
            reader.rdAdminRoles = Convert.ToInt32(txtAdminPoles.Text);
        }
        private void btnAddReader_Click(object sender, EventArgs e)
        {
            SetTextToReader();
            //无需判断这些，因为rdID是主键，唯一值
            /*int rdID = Convert.ToInt32(txtID.Text);
            reader = ReaderAdmin.GetReader(rdID);
            int IsFind = ReaderAdmin.IsFindReader(rdID);//如果读者已经办理读书证，不能重复办理
            //int IsNewReader = ReaderAdmin.IsNewReader(rdID);
            if (IsFind != 0 && "有效".Equals(reader.rdStatus))
                MessageBox.Show("您的读书证有效，不能办理！！！");
            else if(IsFind == 0 || "挂失".Equals(reader.rdStatus))
            {*/
                readerBLL.Add(reader);
                dt = readerBLL.GetAllReader();
                ShowData();
                MessageBox.Show("成功办理借书证！");
            
        }
        private void btnUpdateReader_Click(object sender, EventArgs e)
        {
            SetTextToReader();
            readerBLL.Update(reader);
            dt = readerBLL.GetAllReader();
            ShowData();
            MessageBox.Show("借书证变更成功！");
        }
        private void btnLossDoc_Click(object sender, EventArgs e)
        {
            SetTextToReader();
            readerBLL.Loss(reader);
            dt = readerBLL.GetAllReader();
            ShowData();
            MessageBox.Show("借书证挂失成功！");
        }
        private void btnUnlossDoc_Click(object sender, EventArgs e)
        {
            SetTextToReader();
            readerBLL.UnLoss(reader);
            dt = readerBLL.GetAllReader();
            ShowData();
            MessageBox.Show("成功解除挂失！");
        }

        private void btnCancelDoc_Click(object sender, EventArgs e)
        {
            SetTextToReader();
            readerBLL.Delete(reader);
            MessageBox.Show("借书证成功注销！");
        }
 

        private void picboxPhoto_Click(object sender, EventArgs e)
        {

        }

        private void btnLoadPictureFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Filter = "图片文件(*.jpg; *.bmp; *.png; *.gif) | *.jpg; *.bmp; *.png; *.gif";
            if (ofd1.ShowDialog() == DialogResult.OK)
            {
                Image imgPhoto = Image.FromFile(ofd1.FileName);
                picboxPhoto.Image = imgPhoto;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox2.Controls)//清除所有textbox的内容
            {
                if (ctrl is TextBox)
                    ctrl.Text = " ";
            }
        }

        private void RdBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
